class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& a) {
        int n = a.size();
        vector<int>ans;
        for (int i = 0; i < n; i++) {
            int x = -1e9 - 10;
            bool f = 0;
            for (int j = i + 1; j < n; j++) {
                if (a[i] < a[j]) {
                    x = a[j];
                    f = 1;
                    break;
                }
            }
            if (f)ans.push_back(x);
            else {
                for (int j = 0; j < i; j++) {
                    if (a[i] < a[j]) {
                        x = a[j];
                        f = 1;
                        break;
                    }
                }
                if (f)ans.push_back(x);
                else ans.push_back(-1);
            }
        }
        return ans;
    }
};